Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
JavaScript Higher Order Function keeps returning newArray as [Nan, Nan, Nan] when it should be numbers

I've been working on a problem that involves higher order functions and callbacks. The callback function is below.

**const addTwo = num => num + 2;**

I'm trying to pass this callback into the higher order function below.

**const map = (array, callback) => {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    newArray.push(callback(newArray[i]))
  }
  return newArray;
};**

The variable "newArray," inside of the map function should be returned as an array of 3 different numbers added by two. The arguments for the map function's parameters are below within a console log.

**console.log(map([1, 2, 3], addTwo));**

The addTwo function should become the argument of the callback parameter within the map function, this should make every element in the above array be added by two. I keep seeing [NaN, NaN, NaN] in the console and I'm not quite sure why every number in the index isn't being added by two.

I would appreciate the help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are pushing the values from the new array, when it should be the argument instead

**const map = (array, callback) => {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    newArray.push(callback(array[i]))
  }
  return newArray;
};**
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda